package org.aiesec.fortaleza.xmatch.model.service;

import java.io.Serializable;
import java.util.List;

import org.aiesec.fortaleza.xmatch.model.entity.EntityBase;
import org.aiesec.fortaleza.xmatch.model.exceptions.XMatchDomainException;
import org.aiesec.fortaleza.xmatch.model.exceptions.XMatchMessageException;
import org.aiesec.fortaleza.xmatch.model.exceptions.XMatchDomainException.TypeMessage;
import org.aiesec.fortaleza.xmatch.model.repository.Repository;
import org.aiesec.fortaleza.xmatch.model.util.Criteria;
import org.aiesec.fortaleza.xmatch.model.validator.ValidatorUtil;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.TransactionPropagationType;
import org.jboss.seam.annotations.Transactional;

@Scope(ScopeType.STATELESS)
@Name("service")
public class ServiceImpl<Entity extends EntityBase> implements Service<Entity> {

	@In(create = true)
	protected ValidatorUtil validatorUtil;

	public void setValidatorUtil(ValidatorUtil validatorUtil) {
		this.validatorUtil = validatorUtil;
	}

	private Repository<Entity> repository;

	public Repository<Entity> getRepository() {
		return repository;
	}

	public void setRepository(Repository<Entity> repository) {
		this.repository = repository;
	}

	@Override
	@Transactional
	public List<Entity> findAll() {
		return getRepository().findAll();
	}

	@Override
	@Transactional
	public Entity findById(Serializable k) {
		return getRepository().findBy(k);
	}

	@Override
	@Transactional(TransactionPropagationType.REQUIRED)
	public void remove(Entity e) throws XMatchDomainException {
		try {
			getRepository().remove(e);
		} catch (XMatchDomainException xMatchDomainException) {
			throw xMatchDomainException;
		} catch (Exception e2) {
			e2.printStackTrace();
			throw new XMatchDomainException(new XMatchMessageException(
					"default.error.remove", TypeMessage.ERROR), e2);
		}
	}

	@Override
	@Transactional(TransactionPropagationType.REQUIRED)
	public Entity save(Entity e) throws XMatchDomainException {
		try {
			validatorUtil.validate(e);
			return getRepository().save(e);
		} catch (XMatchDomainException xMatchDomainException) {
			throw xMatchDomainException;
		} catch (Exception e2) {
			e2.printStackTrace();
			throw new XMatchDomainException(new XMatchMessageException(
					"default.error.save", TypeMessage.ERROR), e2);
		}

	}

	@Override
	@Transactional
	public List<Entity> readByCriteria(Criteria criteria, int initialPos,
			int finalPos) {
		return getRepository().readByCriteria(criteria, initialPos, finalPos);
	}

	@Override
	@Transactional
	public List<Entity> readByCriteria(Criteria criteria) {
		return getRepository().readByCriteria(criteria);
	}

	@Override
	@Transactional
	public Long totalOfByCriteria(Criteria criteria) {
		return getRepository().totalOfByCriteria(criteria);
	}

}
